package org.kpli.jak.net;

import java.io.*;
import java.net.*;

public class TCPServer {

	private final int INFO_PORT = 54451;
	private String datafromClient;

	public TCPServer() {
		BufferedReader inFromClient;
		DataOutputStream outToClient;
		Socket serverSocket;
		try {
			ServerSocket infoServer = new ServerSocket(INFO_PORT);
			while (true) {
				serverSocket = infoServer.accept();
				System.out.println("Ada client yang terkoneksi!");
				inFromClient = new BufferedReader(new InputStreamReader(
						serverSocket.getInputStream()));
				outToClient = new DataOutputStream(
						serverSocket.getOutputStream());
				boolean isQUIT = false;
				while (!isQUIT) {
					datafromClient = inFromClient.readLine();
					if (datafromClient.startsWith("QUIT"))
						isQUIT = true;
					else
						outToClient.writeBytes(datafromClient.replaceAll(" ",
								":") + "\n");
				}
				outToClient.close();
				inFromClient.close();
				serverSocket.close();
				System.out.println("Koneksi client tertutup..");
			}
		} catch (IOException ioe) {
			System.out.print("error: " + ioe);
		} catch (Exception e) {
			System.out.print("error: " + e);
		}
	}

	/* program utama */
	public static void main(String[] args) {
		new TCPServer();
	}

}
